﻿/*
* 创建者：LIYINFAI
* 创建时间：2023/10/24 9:35:26
* 描述：
* --------------------------------------
* 修改说明：
* --------------------------------------
*/
using CommonLibrary;
using CommonLibrary.Forms;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Text;

namespace BusinessLibrary
{
    /// <summary>
    /// 产品计数
    /// </summary>
    public class ProductCount : IPlugin
    {
        private static List<PLCTagSetting> PRODUCT_COUNT_TAGS = new List<PLCTagSetting>() {
            new PLCTagSetting(){
                 ORMName=AppPLCSetting.Instance().InboundNumAddress,
                  Tag=AppPLCSetting.Instance().InboundNumAddress,
                  TypeName = EDataType.INT.ToString().ToLower(),
            },
            new PLCTagSetting(){
                 ORMName=AppPLCSetting.Instance().OutboundNumAddress,
                  Tag=AppPLCSetting.Instance().OutboundNumAddress,
                  TypeName = EDataType.INT.ToString().ToLower(),
            },
            new PLCTagSetting(){
                 ORMName=AppPLCSetting.Instance().ScanNGNumAddress,
                  Tag=AppPLCSetting.Instance().ScanNGNumAddress,
                  TypeName = EDataType.INT.ToString().ToLower(),
            },
            //new PLCTagSetting(){
            //     ORMName=AppPLCSetting.Instance().HeliumNumAddress,
            //      Tag=AppPLCSetting.Instance().HeliumNumAddress,
            //      TypeName = EDataType.INT.ToString().ToLower(),
            //},
            new PLCTagSetting(){
                 ORMName=AppPLCSetting.Instance().HeliumNGNumAddress,
                  Tag=AppPLCSetting.Instance().HeliumNGNumAddress,
                  TypeName = EDataType.INT.ToString().ToLower(),
            },
            //new PLCTagSetting(){
            //     ORMName=AppPLCSetting.Instance().PressTestNumAddress,
            //      Tag=AppPLCSetting.Instance().PressTestNumAddress,
            //      TypeName = EDataType.INT.ToString().ToLower(),
            //},
            new PLCTagSetting(){
                 ORMName=AppPLCSetting.Instance().PressTestNGNumAddress,
                  Tag=AppPLCSetting.Instance().PressTestNGNumAddress,
                  TypeName = EDataType.INT.ToString().ToLower(),
            },
            //new PLCTagSetting(){
            //     ORMName=AppPLCSetting.Instance().InsulationTestNumAddress,
            //      Tag=AppPLCSetting.Instance().InsulationTestNumAddress,
            //      TypeName = EDataType.INT.ToString().ToLower(),
            //},
            new PLCTagSetting(){
                 ORMName=AppPLCSetting.Instance().InsulationTestNGNumAddress,
                  Tag=AppPLCSetting.Instance().InsulationTestNGNumAddress,
                  TypeName = EDataType.INT.ToString().ToLower(),
            }
        };
        /// <summary>
        /// 描述信息
        /// </summary>
        public string Description => "产品计数";
        /// <summary>
        /// 执行方法
        /// </summary>
        /// <param name="pluginParam">参数</param>
        /// <returns></returns>
        public string Excute(PluginParamEntity pluginParam)
        {
            string result = string.Empty;
            if (pluginParam != null
                && APPInstance.PLC != null
                && APPInstance.PLC.IsConnected
                && pluginParam.Identity.ToUpper().Equals("PRODUCTCOUNT_PLC"))
            {
                result = ProductCountPLC(pluginParam);
            }
            return result;
        }
        /// <summary>
        /// 返回一个窗口
        /// </summary>
        /// <param name="pluginParam"></param>
        /// <returns></returns>

        public HWFrmWithTitle GetForm(PluginParamEntity pluginParam)
        {
            return null;
        }
        /// <summary>
        /// 返回一个 Bitmap
        /// </summary>
        /// <returns></returns>
        public Bitmap GetBitmap()
        {
            return null;
        }
        /// <summary>
        /// PRODUCTCOUNT_PLC
        /// 产品计数
        /// </summary>
        /// <returns></returns>
        private string ProductCountPLC(PluginParamEntity pluginParam)
        {
            string result = string.Empty;
            AppPLCSetting mAppParam = AppPLCSetting.Instance();
            var data = new ProductCountExcuteEntity();
            string methodName = "产品计数";
            this.Info($"【{pluginParam.Tag}】开始 - {methodName}");
            try
            {
                if (APPInstance.PLC != null && APPInstance.PLC.IsConnected)
                {
                    var values = APPInstance.PLC.BatchRead(PRODUCT_COUNT_TAGS, true);
                    if (values != null && values.Keys != null && values.Keys.Count > 0)
                    {
                        data.InboundNum = (int)values[mAppParam.InboundNumAddress];
                        data.OutboundNum = (int)values[mAppParam.OutboundNumAddress];
                        data.ScanNGNum = (int)values[mAppParam.ScanNGNumAddress];
                        data.HeliumNGNum = (int)values[mAppParam.HeliumNGNumAddress];
                        data.PressTestNGNum = (int)values[mAppParam.PressTestNGNumAddress];
                        data.InsulationTestNGNum = (int)values[mAppParam.InsulationTestNGNumAddress];
                        data.HeliumNum = data.OutboundNum;
                        data.PressTestNum = data.OutboundNum;
                        data.InsulationTestNum = data.OutboundNum;
                    }
                }
                result = CommonHelper.GetJsonString(data, Newtonsoft.Json.Formatting.None);
            }
            catch (Exception ex)
            {
                this.Error(ex);
            }
            result = CommonHelper.GetJsonString(data, Newtonsoft.Json.Formatting.None);
            this.Info($"【{pluginParam.Tag}】结束 - {methodName}，结果：{result}");
            return result;
        }
    }
}
